package com.moshang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.entity.ScheduleJob;
import com.moshang.blog.entity.bo.JobOperateEnum;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.mapper.ScheduleJobMapper;
import com.moshang.blog.quartz.service.QuartzService;
import com.moshang.blog.service.ScheduleJobService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author xww
 * Created on 2019/4/25.
 */
@Service
@Transactional
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobMapper,ScheduleJob> implements ScheduleJobService {
    @Autowired
    private ScheduleJobMapper scheduleJobMapper;
    @Autowired
    private QuartzService quartzService;

    @Override
    public void add(ScheduleJob job) {

        //此处省去数据验证
        baseMapper.insert(job);

        //加入job
        try {
            quartzService.addJob(job);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start(int id) {
        //此处省去数据验证
        ScheduleJob job = baseMapper.selectById(id);
        job.setStatus(1);
        this.updateById(job);

        //执行job
        try {
            quartzService.operateJob(JobOperateEnum.START, job);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pause(int id) {
        //此处省去数据验证
        ScheduleJob job = baseMapper.selectById(id);
        job.setStatus(2);
        this.updateById(job);

        //执行job
        try {
            quartzService.operateJob(JobOperateEnum.PAUSE, job);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(int id) {
        //此处省去数据验证
        ScheduleJob job = baseMapper.selectById(id);
        this.getById(id);

        //执行job
        try {
            quartzService.operateJob(JobOperateEnum.DELETE, job);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void startAllJob() {
        //此处省去数据验证
        ScheduleJob job = new ScheduleJob();
        job.setStatus(1);
        this.update(job, new QueryWrapper<>());

        //执行job
        try {
            quartzService.startAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pauseAllJob() {
        //此处省去数据验证
        ScheduleJob job = new ScheduleJob();
        job.setStatus(2);
        this.update(job, new QueryWrapper<>());

        //执行job
        try {
            quartzService.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void saveOrUpdateDict(ScheduleJob scheduleJob) {

        if (scheduleJob.getId()==null) {

            scheduleJob.setCreateId(MySysUser.currentUser().getId());
            scheduleJob.setCreateName(MySysUser.currentUser().getRealName());
            scheduleJob.setDeleteFlag(false);
            scheduleJob.setUpdateTime(new Date());
            scheduleJob.setCreateTime(new Date());
            scheduleJob.setStatus(1);
            save(scheduleJob);
            quartzService.startJob(scheduleJob);
        }else {
            ScheduleJob scheduleJob1=scheduleJobMapper.selectById(scheduleJob.getId());
            scheduleJob1.setUpdateTime(new Date());
            updateById(scheduleJob1);
//            if (scheduleJob1.getStatus()==1){
//                quartzService.updateJob(scheduleJob1);
//                quartzService.startJob(scheduleJob1);
//            }else {
//             quartzService.remove(scheduleJob1);
//            }
        }
    }
}
